package cn.bs.busuo.front.content.service.impl;

import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.front.content.dao.cache.UserSightArticleCacheRepository;
import cn.bs.busuo.front.content.dao.persist.repository.UserSightArticle;
import cn.bs.busuo.front.content.pojo.entity.ContentHotelArticleDetail;
import cn.bs.busuo.front.content.pojo.entity.ContentSightArticle;
import cn.bs.busuo.front.content.pojo.entity.ContentSightArticleDetail;
import cn.bs.busuo.front.content.pojo.param.SightArticleAddNewParam;
import cn.bs.busuo.front.content.pojo.param.SightArticleUpDateParam;
import cn.bs.busuo.front.content.pojo.vo.ContentHotelArticleByTitleVO;
import cn.bs.busuo.front.content.pojo.vo.ContentSightArticleByTitleVO;
import cn.bs.busuo.front.content.pojo.vo.ContentSightArticleDetailByIdVO;
import cn.bs.busuo.front.content.pojo.vo.UserContentSightArticleByIdVO;
import cn.bs.busuo.front.content.service.UserSightArticleService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Feng
 * @since: 2023/12/8
 */
@Service
public class UserSightArticleServiceImpl implements UserSightArticleService {

    @Value("${busuo.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private UserSightArticle userSightArticle;
    @Autowired
    private UserSightArticleCacheRepository userSightArticleCacheRepository;

    //用户添加景点文章
    @Override
    public void addNewSightArticle(CurrentPrincipal currentPrincipal, SightArticleAddNewParam sightArticleAddNewParam,Long id) {
        //插入到酒店文章
        ContentSightArticle contentSightArticle = new ContentSightArticle();
        contentSightArticle.setBrief(sightArticleAddNewParam.getBrief());
        contentSightArticle.setCoverUrl(sightArticleAddNewParam.getCoverUrl());
        contentSightArticle.setSightsId(id);
        contentSightArticle.setKeywords(sightArticleAddNewParam.getKeywords());
        String title = sightArticleAddNewParam.getTitle();
        contentSightArticle.setTitle(title);

        //插入当事人信息
        contentSightArticle.setAuthorId(currentPrincipal.getId());
        contentSightArticle.setAuthorName(currentPrincipal.getUsername());

        //插入到景点酒店详情实体类
        ContentSightArticleDetail contentSightArticleDetail = new ContentSightArticleDetail();
        contentSightArticleDetail.setDetail(sightArticleAddNewParam.getDetail());

        //调用添加方法
        addSlightArticle(contentSightArticleDetail,contentSightArticle,title);

    }

    //用户删除景点文章,设置为不显示
    @Override
    public void deleteArticle(CurrentPrincipal currentPrincipal,Long id) {
        //判断该文章是否存在
        ContentSightArticle contentSightArticle = userSightArticle.selectSightArticleById(id);
        if (contentSightArticle == null){
            String msg = "该文章不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        //判断景点文章作者是否是当前登录人
        if (!contentSightArticle.getAuthorName().equals(currentPrincipal.getUsername())){
            String msg = "该文章不是您发布的";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,msg);
        }
        userSightArticle.updateSightArticleById(id);
        //在redis里面修改文章状态
        userSightArticleCacheRepository.updateArticle(id);
    }

    //根据景点Id查询
    @Override
    public String selectSightArticleById(Long id) {
        List<UserContentSightArticleByIdVO> userContentSightArticleByIdVO = userSightArticle.selectSightArticleBySightId(id);
        if (userContentSightArticleByIdVO.size() == 0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该景点没有文章");
        }
        String json = JSON.toJSON(userContentSightArticleByIdVO).toString();
        return json;
    }

    @Override
    public ContentSightArticleDetailByIdVO selectSightArticleDetailById(Long id) {
        ContentSightArticleDetailByIdVO contentSightArticleDetailByIdVO = userSightArticle.selectSightArticleDetailById(id);
        if (contentSightArticleDetailByIdVO == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
        }
        //增加浏览量
        userSightArticle.increaseClickCount(id);
        return contentSightArticleDetailByIdVO;
    }

    //用户查询自己酒店文章
    @Override
    public String selectUserListSlightArticle(String authorName) {
        List<UserContentSightArticleByIdVO> userListSightArticle = userSightArticle.selectUserListSightArticle(authorName);
        if (userListSightArticle.size() == 0){
            throw new ServiceException(ServiceCode.OK,"没有文章,快去写文章吧");
        }
        String json = JSON.toJSON(userListSightArticle).toString();
        return json;
    }

    //查询景点文章详情并好评
    @Override
    public void increaseUpCount(Long id) {
        ContentSightArticleDetailByIdVO contentSightArticleDetailByIdVO = userSightArticle.selectSightArticleDetailById(id);
        if (contentSightArticleDetailByIdVO == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
        }
        //点赞
        int i = userSightArticle.increaseUpCount(id);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"点赞失败");
        }
    }

    //查询景点文章详情并差评
    @Override
    public void increaseDownCount(Long id) {
        ContentSightArticleDetailByIdVO contentSightArticleDetailByIdVO = userSightArticle.selectSightArticleDetailById(id);
        if (contentSightArticleDetailByIdVO == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
        }
        //差评
        int i = userSightArticle.increaseDownCount(id);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"点赞失败");
        }
    }

    //修改自己的景点文章
    @Override
    public void upDateSightArticle(String authorName, Long id, SightArticleUpDateParam sightArticleUpDateParam) {
        ContentSightArticle contentSightArticle = userSightArticle.selectSightArticleById(id);
        if (contentSightArticle == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
        }
        if (!authorName.equals(contentSightArticle.getAuthorName())){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"很抱歉,这篇文章不是您写的");
        }
        //查询是否有详情
        ContentHotelArticleDetail contentHotelArticleDetail = userSightArticle.selectSightArticleDetail(id);
        if (contentHotelArticleDetail == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有该文章的详情");
        }
        //判断该文章标题是否重复
        int title = userSightArticle.contentSightArticleByTitle(sightArticleUpDateParam.getTitle());
        if (title != 0) {
            String msg = "该文章标题已存在,请重新输入";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }


        //调用修改文章的IO方法
        upDataArticle(id,sightArticleUpDateParam);
    }

    @Transactional
    public void upDataArticle(Long id, SightArticleUpDateParam sightArticleUpDateParam) {
        String newTitle = sightArticleUpDateParam.getTitle();
        String newBrief = sightArticleUpDateParam.getBrief();
        String newCoverUrl = sightArticleUpDateParam.getCoverUrl();
        String newKeywords = sightArticleUpDateParam.getKeywords();

        //修改文章表
        int i = userSightArticle.updateSightArticle(newTitle, newBrief, newCoverUrl, newKeywords, id);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"修改失败");
        }

        //修改详情表
        int j = userSightArticle.updateHotelArticleDetail(sightArticleUpDateParam.getDetail(), id);
        if (j != 1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"修改详情失败");
        }
    }


    //添加景点文章IO
    @Transactional
    public void addSlightArticle(ContentSightArticleDetail contentSightArticleDetail, ContentSightArticle contentSightArticle,String title) {
        //判断该文章标题是否重复
        int titles = userSightArticle.contentSightArticleByTitle(contentSightArticle.getTitle());
        if (titles != 0) {
            String msg = "该文章标题已存在,请重新输入";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        //添加到景点文章
        int i = userSightArticle.addNewSightArticle(contentSightArticle);
        if (i != 1){
            String msg = "发布景点文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }

        //存进redis景点文章表
        ContentSightArticleByTitleVO contentSightArticleByTitleVO = userSightArticle.selectSightArticleByTitleVO(title);
        userSightArticleCacheRepository.addNewSightArticle(contentSightArticleByTitleVO.getId(),contentSightArticleByTitleVO);

        //从景点文章里面获取文章Id
        Long NewArticleId = userSightArticle.selectSightArticleByTitle(contentSightArticle.getTitle());
        contentSightArticleDetail.setSightArticleId(NewArticleId);

        //添加到景点文章详情
        int j = userSightArticle.addNewSightArticleDetail(contentSightArticleDetail);
        if (j != 1){
            String msg = "发布景点文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }

    }
}
